package mundo.java.client.view;

import mundo.java.client.Artigo.Messages;
import mundo.java.client.events.FailureEvent;
import mundo.java.client.events.GreetingResponseEvent;
import mundo.java.client.events.MessageValidatedEvent;
import mundo.java.client.events.Event.Handler;
import mundo.java.shared.GreetingAction;
import mundo.java.shared.GreetingResponse;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.inject.Inject;

public class ArtigoDialogView extends ArtigoAbstractView {
	final DialogBox dialogBox = new DialogBox();
	final Label textToServerLabel = new Label();
	final HTML serverResponseLabel = new HTML();
	final Button closeButton = new Button(messages.closeButtonLabel());

	@Inject
	public ArtigoDialogView(HandlerManager eventBus, Messages messages) {
		super(eventBus, messages);
		
		eventBus.addHandler(MessageValidatedEvent.TYPE, new Handler<MessageValidatedEvent>() {
			@Override
			public void onEvent(MessageValidatedEvent event) {
				if (event.messageValidated) {
					textToServerLabel.setText(event.action.getTextToServer());
					serverResponseLabel.setText("");
				}
			}
		});
		
		eventBus.addHandler(FailureEvent.TYPE, new Handler<FailureEvent>() {
			@Override
			public void onEvent(FailureEvent event) {
				showFailure();
			}
		});
		
		eventBus.addHandler(GreetingResponseEvent.TYPE, new Handler<GreetingResponseEvent>() {
			@Override
			public void onEvent(GreetingResponseEvent event) {
				showGreeting(event.action, event.response);
			}
		});
		
		buildUi();
		render();
	}
	
	private void buildUi() {
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");

		dialogVPanel.add(new HTML("<b>" + messages.sendMessage() + "</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>" + messages.replyMessage() + "</b>"));
		dialogVPanel.add(serverResponseLabel);
		
		dialogVPanel.add(closeButton);
		dialogVPanel.setCellHorizontalAlignment(closeButton, VerticalPanel.ALIGN_RIGHT);
		
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");

		dialogBox.setText(messages.rpcLabel()); // dialog header
		dialogBox.setAnimationEnabled(true);
		dialogBox.setWidget(dialogVPanel); // dialog content
		
		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				sendButton.setEnabled(true);
				sendButton.setFocus(true);
			}
		});
	}

	@Override
	public String greet(String text, String serverInfo, String userAgent) {
		return messages.greeting(text) + "!<br><br>" +
			messages.serverInfoGreeting(serverInfo) + "<br><br>" +
			messages.userAgentGreeting() + "<br>" + userAgent;
	}

	public void showFailure() {
		// Show the RPC error message to the user
		dialogBox.setText(messages.rpcFailure());
		serverResponseLabel.addStyleName("serverResponseLabelError");
		serverResponseLabel.setHTML(messages.serverError());
		dialogBox.center();
		closeButton.setFocus(true);
	}

	public void showGreeting(GreetingAction action, GreetingResponse response) {
		String text = action.getTextToServer();
		String serverInfo = response.getServerInfo();
		String userAgent = response.getUserAgent();
		
		dialogBox.setText(messages.rpcLabel());
		serverResponseLabel.removeStyleName("serverResponseLabelError");
		serverResponseLabel.setHTML(greet(text, serverInfo, userAgent));
		dialogBox.center();
		closeButton.setFocus(true);
	}
}